home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / Guide.aspx.cs367 < prev    next >
Text File  |  2008-02-23  |  20KB  |  564 lines

  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Web;
  5. using System.Web.Caching;
  6. using System.Web.UI;
  7. using System.Web.UI.HtmlControls;
  8. using System.Web.UI.WebControls;
  9.  
  10. using GBPVR.Public;
  11. using GBPVRSchedule;
  12. using gbweb.classes;
  13.  
  14. namespace gbweb
  15. {
  16.     /// <summary>
  17.     /// Summary description for guide.
  18.     /// </summary>
  19.     public partial class guide : System.Web.UI.Page
  20.     {
  21.         protected System.Web.UI.HtmlControls.HtmlImage IMG4;
  22.         protected System.Web.UI.HtmlControls.HtmlImage IMG5;
  23.  
  24.         private DateTime startTime;
  25.         private Settings guideParams;
  26.  
  27.         //Guide display values that are stored in the users cookie
  28.         private static int minuteSpan;
  29.         private static int minuteInterval;
  30.         private static int minuteSlice;
  31.         private static int TimelineRepeat;
  32.         private static string useChannelIcons;
  33.         private static string channelIconHeight;
  34.         private static string channelIconWidth;
  35.  
  36.         protected void Page_Load(object sender, System.EventArgs e)
  37.         {
  38.             guideParams = Global.Settings;
  39.             Session["GuideStartTime"] = null;
  40.             GetCookieValues();
  41.           
  42.             if (!IsPostBack)
  43.             {
  44.                 // populate date/time navigation controls
  45.                 DateTime today = DateTime.Today;          
  46.  
  47.                 for (int hours=0; hours < 24; hours++)
  48.                 {
  49.                     DateTime theHour = today.AddHours(hours);
  50.                     for (int minutes=0; minutes < 60; minutes=minutes + minuteInterval)
  51.                     {
  52.                         DateTime theMinute = theHour.AddMinutes(minutes);
  53.                         ListItem myLI = new ListItem(theMinute.ToShortTimeString(), theMinute.ToString("HH:mm"));
  54.                         timeJump.Items.Add(myLI);
  55.                     }
  56.                 }
  57.                 Global.FillGenreList(genreList);
  58.             }
  59.  
  60.             if (ViewState["GuideStartTime"] == null)
  61.             {
  62.                 if (guideParams.guideStartTime != "")
  63.                 {
  64.                     // default the start time to the configured default start time
  65.                     startTime = Convert.ToDateTime(DateTime.Now.ToShortDateString() + " " + guideParams.guideStartTime);
  66.                 }
  67.                 else
  68.                 {
  69.                     // default to current time if the user has just logged in, or come from the 'manage recordings' page
  70.                     startTime = DateTime.Now;
  71.                 }
  72.  
  73.                 // remove seconds and miliseconds
  74.                 startTime = startTime.AddSeconds(-startTime.Second);
  75.                 startTime = startTime.AddMilliseconds(-startTime.Millisecond);
  76.             }
  77.             else
  78.             {
  79.                 startTime = (DateTime)ViewState["GuideStartTime"];
  80.             }
  81.  
  82.             if (Request.Form["__EVENTTARGET"] != null)
  83.             {
  84.                 if (Request.Form["__EVENTTARGET"].IndexOf("viewleftbutton") != -1) ViewLeftButton_Click(null, null);
  85.                 if (Request.Form["__EVENTTARGET"].IndexOf("viewrightbutton") != -1) ViewRightButton_Click(null, null);
  86.             }
  87.  
  88.         }
  89.  
  90.         #region Web Form Designer generated code
  91.         override protected void OnInit(EventArgs e)
  92.         {
  93.             //
  94.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  95.             //
  96.             InitializeComponent();
  97.             base.OnInit(e);
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Required method for Designer support - do not modify
  102.         /// the contents of this method with the code editor.
  103.         /// </summary>
  104.         private void InitializeComponent()
  105.         {
  106.  
  107.         }
  108.         #endregion
  109.  
  110.         protected void GoButton_Click(object sender, System.EventArgs e)
  111.         {
  112.             startTime = DateTime.Parse(dateJump.SelectedValue + " " + timeJump.SelectedValue);
  113.         }
  114.  
  115.         private void ViewLeftButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  116.         {
  117.             startTime = startTime.AddMinutes(-minuteSpan);
  118.         }
  119.  
  120.         private void ViewRightButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  121.         {
  122.             startTime = startTime.AddMinutes(minuteSpan);
  123.         }
  124.  
  125.         protected void Page_PreRender(object sender, System.EventArgs e)
  126.         {
  127.  
  128.             Schedule scheduleHelper = Global.Schedule;
  129.             ProgrammeDisplay displayProgramme = new ProgrammeDisplay();
  130.  
  131.             // build up a list of the recording we already know about
  132.             IDictionary knownRecordings = scheduleHelper.LoadKnownRecordings();
  133.  
  134.             // normalize time period
  135.             startTime = startTime.AddMinutes(-startTime.Minute % minuteInterval);
  136.             DateTime endTime = startTime.AddMinutes(minuteSpan);
  137.  
  138.             viewingDate.Text = startTime.ToLongDateString();
  139.  
  140.             // store start time away for later use
  141.             ViewState["GuideStartTime"] = startTime;
  142.  
  143.             // select the correct minutes setting
  144.             //timeJump.SelectedIndex = (int)(startTime.TimeOfDay.TotalMinutes / 30);
  145.             timeJump.SelectedValue = startTime.ToString("HH:mm");
  146.             // select correct date
  147.             //dateJump.SelectedIndex = (int)(startTime.Date.DayOfYear - DateTime.Now.AddDays(-1).Date.DayOfYear);
  148.             /*if (dateJump.Items.FindByValue(startTime.ToShortDateString()) == null)
  149.             {
  150.                 // add the item at the start of the list if it's before today, otherwise at the end
  151.                 // this way we keep a 'sorted' list
  152.                 if (startTime < DateTime.Today)
  153.                     dateJump.Items.Insert(0, new ListItem(startTime.ToLongDateString(), startTime.ToShortDateString()));
  154.                 else
  155.                     dateJump.Items.Add(new ListItem(startTime.ToLongDateString(), startTime.ToShortDateString()));
  156.             }*/
  157.             int listoffset = (startTime.Date - DateTime.Today).Days;
  158.             if (listoffset < 0) listoffset = 0;
  159.             if (listoffset > 6) listoffset = 6;
  160.             DateTime jumpDate = startTime.AddDays(-listoffset);
  161.  
  162.             dateJump.Items.Clear();
  163.             for (int nCount=-1; nCount<=14; nCount++)
  164.             {
  165.                 DateTime theDay = jumpDate.AddDays(nCount);
  166.                 ListItem myLI = new ListItem(theDay.ToLongDateString(), theDay.ToShortDateString());
  167.                 dateJump.Items.Add(myLI);
  168.             }
  169.  
  170.             dateJump.SelectedValue = startTime.ToShortDateString();
  171.  
  172.  
  173.             // retrieve listings for time period
  174.             IList listingsForPeriod;
  175.             //listingsForPeriod = scheduleHelper.GetListingsForTimePeriod(startTime, endTime);
  176.  
  177.             /* SELECT 
  178.              *    c.oid as c_oid, 
  179.              *    p.oid as p_oid, 
  180.              *    c.name as c_name, 
  181.              *    p.name as p_name, 
  182.              *    * 
  183.              * FROM 
  184.              *    CHANNEL c, 
  185.              *    PROGRAMME p 
  186.              * where 
  187.              *    p.channel_oid = c.oid and    
  188.              *    exists (select 1 from SOURCE_CHANNEL_MAPPING scm where scm.channel_oid = c.oid  and enabled='Y') and   
  189.              *    start_time < @endTime and    
  190.              *    end_time >= @startTime  
  191.              * order by 
  192.              *    channel_number, 
  193.              *    p.start_time
  194.              * */
  195.  
  196.             // ChannelFactory::getShowsInTimePeriod filter is:
  197.             //  start_time < @endtime and end_time >= @starttime
  198.             // and it should be:
  199.             //  start_time < @endtime and end_time > @starttime
  200.             // so that you actually get programs that start _before_ to the endtime, and end _after_ the starttime
  201.             //  we're half fixing this by applying a 1 second correction here, to 'fix' the problem
  202.             listingsForPeriod = scheduleHelper.GetListingsForTimePeriod(startTime.AddSeconds(1), endTime);
  203.             ((ArrayList)listingsForPeriod).Sort(new ChannelNumberComparer());
  204.  
  205.             CaseInsensitiveComparer caseInsensitiveComparer = new CaseInsensitiveComparer();
  206.             string genreFilter = genreList.SelectedValue;
  207.             if ((genreFilter != null) && (genreFilter.Length == 0)) genreFilter = null;
  208.  
  209.             int rowcount = 0;
  210.             // add a table row for each channel
  211.             foreach (Channel channel in listingsForPeriod)
  212.             {
  213.  
  214.                 TableRow channelRow = new TableRow();
  215.                 channelRow.CssClass = "guide_listing_row";
  216.  
  217.                 // add channel name & number
  218.                 TableCell colChannel = new TableCell();
  219.                 colChannel.CssClass = "guide_chnl_cell";
  220.                 string channelName = channel.getName();
  221.                 if (channelName.StartsWith(channel.getChannelNumber().ToString())) // cosmetic stuff for my american friends
  222.                 {
  223.                     if (channelName != channel.getChannelNumber().ToString())
  224.                     {
  225.                         channelName = channelName.Substring(channel.getChannelNumber().ToString().Length + 1);
  226.                     }
  227.                     else
  228.                     {
  229.                         channelName = "";
  230.                     }
  231.                 }
  232.                 colChannel.Width = Unit.Percentage(1); // this will break if the tabel ever uses fixed layout
  233.                 colChannel.HorizontalAlign = HorizontalAlign.Center;
  234.                 string chnl = channel.getChannelNumber().ToString();
  235.  
  236.                 string channelIcon = GetChannelIcon(channel.getChannelNumber(), new string[] {channel.getName(), channelName});
  237.                 if (channelIcon != null)
  238.                 {
  239.                     colChannel.Attributes.Add("class", "channelicon");
  240.                     HyperLink channelURL = new HyperLink();
  241.                     colChannel.Controls.Add(channelURL);
  242.                     channelURL.NavigateUrl = "SearchResults.aspx?listChannels=" + chnl + "&srtOrder=title";
  243.                     Session["SearchResultsOption"] = "0";
  244.                     Image channelImage = new Image();
  245.                     channelURL.Controls.Add(channelImage);
  246.                     channelImage.ToolTip = chnl + " - " + channelName;
  247.                     if (channelIconHeight.Length > 0) channelImage.Height = Unit.Pixel(Convert.ToInt16(channelIconHeight));
  248.                     if (channelIconWidth.Length > 0) channelImage.Width = Unit.Pixel(Convert.ToInt16(channelIconWidth));
  249.                     channelImage.ImageUrl = Download.GetDownloadUrl(false, true, Download.InternalFiles.ChannelIcon, Server.UrlEncode(channelIcon));
  250.                     if (useChannelIcons == "chnlBoth")
  251.                     {
  252.                         TableCell channelText = new TableCell();
  253.                         colChannel.Controls.Add(channelText);
  254.                         channelText.Text =
  255.                          "<span class=\"channelnumber\"><a href=\"SearchResults.aspx?listChannels=" + chnl + "&srtOrder=title" + "\"><b>" + chnl + "</b></a></span><br />" +
  256.                          "<span class=\"channelname\"><a href=\"SearchResults.aspx?listChannels=" + chnl + "&srtOrder=title" + "\"><nobr>" + channelName + "</nobr></a></span>";
  257.                     }
  258.                 }
  259.                 else
  260.                 {
  261.                     Session["SearchResultsOption"] = "0";
  262.                     if (useChannelIcons == "chnlBoth")
  263.                     {
  264.                         TableCell channelText = new TableCell();
  265.                         colChannel.Controls.Add(channelText);
  266.                         channelText.Text =
  267.                          "<span class=\"channelnumber\"><a href=\"SearchResults.aspx?listChannels=" + chnl + "&srtOrder=title" + "\"><b>" + chnl + "</b></a></span><br />" +
  268.                          "<span class=\"channelname\"><a href=\"SearchResults.aspx?listChannels=" + chnl + "&srtOrder=title" + "\"><nobr>" + channelName + "</nobr></a></span>";
  269.                     }
  270.                     else
  271.                     {
  272.                         colChannel.Text =
  273.                             "<span class=\"channelnumber\"><a href=\"SearchResults.aspx?listChannels=" + chnl + "&srtOrder=title" + "\"><b>" + chnl + "</b></a></span><br />" +
  274.                             "<span class=\"channelname\"><a href=\"SearchResults.aspx?listChannels=" + chnl + "&srtOrder=title" + "\"><nobr>" + channelName + "</nobr></a></span>";
  275.                     }
  276.                 }
  277.  
  278.                 channelRow.Cells.Add(colChannel);
  279.  
  280.                 DateTime prevEndTime = startTime;
  281.                 int showcount = 0;
  282.                 // show programmes
  283.                 foreach (Programme programme in channel.getProgrammeList())
  284.                 {
  285.                     int startMinutes = 0;
  286.                     int endMinutes = 0;
  287.                     DateTime programStartTime = programme.getStartTime();
  288.  
  289.                     // check to see if there are 'gaps' in the schedule that we need to pad
  290.                     if (prevEndTime < programStartTime)
  291.                     {
  292.                         TimeSpan stimespan = prevEndTime.Subtract(startTime);
  293.                         startMinutes = (int)Math.Round(stimespan.TotalMinutes);
  294.  
  295.                         TimeSpan etimespan = programme.getStartTime().Subtract(startTime);
  296.                         endMinutes = (int)Math.Round(etimespan.TotalMinutes);
  297.  
  298.                         TableCell paddingCell = new TableCell();
  299.                         paddingCell.ColumnSpan = (endMinutes - startMinutes) / minuteSlice;
  300.                         paddingCell.CssClass = "tborder";
  301.                         channelRow.Cells.Add(paddingCell);
  302.                     }
  303.                     else if (prevEndTime > programStartTime)
  304.                     {
  305.                         // this allows for programs that overlap, we just let the previous program spill into the next
  306.                         //  and the later program looks like it starts later
  307.                         programStartTime = prevEndTime;
  308.                     }
  309.  
  310.                     // calculate start minutes
  311.                     if (programStartTime <= startTime)
  312.                     {
  313.                         startMinutes = 0;
  314.                     }
  315.                     else
  316.                     {
  317.                         TimeSpan timespan = programStartTime.Subtract(startTime);
  318.                         startMinutes = (int)Math.Round(timespan.TotalMinutes);
  319.                     }
  320.  
  321.                     // calculate end minutes
  322.                     if (programme.getEndTime() >= endTime)
  323.                     {
  324.                         endMinutes = minuteSpan;
  325.                     }
  326.                     else
  327.                     {
  328.                         TimeSpan timespan = programme.getEndTime().Subtract(startTime);
  329.                         endMinutes = (int)Math.Round(timespan.TotalMinutes);
  330.                     }
  331.  
  332.                     // check for programs that we can't display,
  333.                     //  they may either be too narrow, or
  334.                     //  they overlap with other programs
  335.                     if ((endMinutes - startMinutes) / minuteSlice < 1) continue;
  336.  
  337.                     prevEndTime = programme.getEndTime();
  338.  
  339.                     // add programme
  340.                     TableCell programmeCell = new TableCell();
  341.                     programmeCell.ColumnSpan = (endMinutes - startMinutes) / minuteSlice;
  342.                     programmeCell.VerticalAlign = VerticalAlign.Top;
  343.  
  344.                     bool showProgramme = true;
  345.                     if (genreFilter != "Choose Genre...")
  346.                     {
  347.                         if (programme.getGenreList().Count > 0)
  348.                         {
  349.                             ArrayList showGenres = new ArrayList(programme.getGenreList());
  350.                             showGenres.Sort();
  351.                             int idx = showGenres.BinarySearch(genreList.SelectedValue, caseInsensitiveComparer);
  352.                             showProgramme = idx > -1;
  353.                         }
  354.                         else
  355.                         {
  356.                             showProgramme = false;
  357.                         }
  358.                     }
  359.  
  360.                     if (showProgramme) 
  361.                     {
  362.                         ScheduledRecording scheduledRecording = null;
  363.                         if (knownRecordings.Contains(programme.getOID())) scheduledRecording = (ScheduledRecording)knownRecordings[programme.getOID()];
  364.                         displayProgramme.FillProgrammeDisplay(Server, programmeCell, programme, scheduledRecording, true);
  365.                         showcount++;
  366.                     }
  367.  
  368.                     // add programme
  369.                     channelRow.Cells.Add(programmeCell);
  370.                 }
  371.  
  372.                 // add row to table
  373.                 if (showcount > 0)
  374.                 {
  375.                     if (rowcount % TimelineRepeat == 0)
  376.                     {
  377.                         // put table header
  378.                         guideView.Rows.Add(createHeaderRow());
  379.                     }
  380.                     rowcount++;
  381.                     guideView.Rows.Add(channelRow);
  382.                 }
  383.                 
  384.             }
  385.             guideView.Rows.Add(createHeaderRow());
  386.             displayProgramme.Dispose();
  387.         }
  388.  
  389.         private static string channelIconPath;
  390.         private static string[] channelIconExtensions;
  391.         private string GetChannelIcon(int channelNumber, string[] channelNames)
  392.         {
  393.             if (useChannelIcons == "chnlIcon" || useChannelIcons == "chnlBoth")
  394.             {
  395.  
  396.                 // Get the Channel Icon Directory
  397.                 if (channelIconPath == null)
  398.                 {
  399.                     lock (typeof(guide))
  400.                     {
  401.                         if (channelIconPath == null) 
  402.                         {
  403.                             channelIconPath = Path.Combine(Global.Settings.GetInstallDir(), @"media\ChannelLogos");
  404.                             channelIconExtensions = Global.Settings.channelIconExtensions.Split(',');
  405.                         }
  406.                     }
  407.                 }
  408.  
  409.                 Hashtable channelIconCache = (Hashtable)Cache["channelIconCache"];
  410.                 if (channelIconCache == null)
  411.                 {
  412.                     lock (typeof(guide))
  413.                     {
  414.                         channelIconCache = (Hashtable)Cache["channelIconCache"];
  415.                         if (channelIconCache == null)
  416.                         {
  417.                             channelIconCache = new Hashtable();
  418.                             Cache.Add(
  419.                                 "channelIconCache", 
  420.                                 channelIconCache, 
  421.                                 new CacheDependency(channelIconPath), 
  422.                                 DateTime.MaxValue, 
  423.                                 TimeSpan.Zero, 
  424.                                 CacheItemPriority.Normal, 
  425.                                 null);
  426.                         }
  427.                     }
  428.                 }
  429.  
  430.                 if (channelIconCache.ContainsKey(channelNumber))
  431.                 {
  432.                     return (string)channelIconCache[channelNumber];
  433.                 }
  434.  
  435.                 lock (channelIconCache)
  436.                 {
  437.                     if (channelIconCache.ContainsKey(channelNumber))
  438.                     {
  439.                         return (string)channelIconCache[channelNumber];
  440.                     }
  441.  
  442.                     string channelIconFile = null;
  443.                     foreach (string channelName in channelNames)
  444.                     {
  445.                         foreach (string channelIconExtension in channelIconExtensions)
  446.                         {
  447.                             string cleanName = channelName;
  448.                             string channelNameWork = channelName;
  449.                             while (cleanName.Contains("/"))
  450.                             {
  451.                                 if (channelNameWork.Contains("/"))
  452.                                 {
  453.                                     cleanName = channelNameWork.Remove(channelNameWork.IndexOf("/"), 1);
  454.                                     channelNameWork = cleanName;
  455.                                 }
  456.                             }
  457.                             string probeFile = cleanName + "." + channelIconExtension;
  458.                             if (File.Exists(Path.Combine(channelIconPath, probeFile)))
  459.                             {
  460.                                 channelIconFile = probeFile;
  461.                                 break;
  462.                             }
  463.                         }
  464.                         if (channelIconFile != null) break;
  465.                     }
  466.                     channelIconCache[channelNumber] = channelIconFile;
  467.                     return channelIconFile;
  468.                 }
  469.             }
  470.             return null;
  471.         }
  472.  
  473.         private TableRow createHeaderRow()
  474.         {
  475.             return new TableHeaderRow(guideParams, startTime);
  476.         }
  477.  
  478.         private class ChannelNumberComparer : IComparer
  479.         {
  480.  
  481.             #region IComparer Members
  482.  
  483.             public int Compare(object x, object y)
  484.             {
  485.                 return ((Channel)x).channelNumber.CompareTo(((Channel)y).channelNumber);
  486.             }
  487.  
  488.             #endregion
  489.  
  490.         }
  491.  
  492.         private class TableHeaderRow : TableRow, INamingContainer
  493.         {
  494.  
  495.             public TableHeaderRow(Settings guideParams, DateTime startTime)
  496.             {
  497.                 TableRow headerRow = this;
  498.  
  499.                 TableCell colChannel = new TableCell();
  500.                 colChannel.CssClass = "header";
  501.                 colChannel.Wrap = false;
  502.                 headerRow.Cells.Add(colChannel);
  503.                 if (useChannelIcons == "chnlBoth")
  504.                 {
  505.                     colChannel = new TableCell();
  506.                     headerRow.Cells.Add(colChannel);
  507.                     colChannel.CssClass = "header";
  508.                 }
  509.  
  510.                 for (int mins = 0; mins < minuteSpan; mins += minuteInterval)
  511.                 {
  512.                     TableCell colTimeslot = new TableCell();
  513.                     colTimeslot.ColumnSpan = minuteInterval / minuteSlice;
  514.                     colTimeslot.CssClass = "header";
  515.                     colTimeslot.Text = startTime.AddMinutes(mins).ToShortTimeString();
  516.                     colTimeslot.Wrap = false;
  517.                     headerRow.Cells.Add(colTimeslot);
  518.                 }
  519.  
  520.                 addViewButton(headerRow.Cells[0], "left", "<<");
  521.                 addViewButton(headerRow.Cells[headerRow.Cells.Count - 1], "right", ">>");
  522.             }
  523.  
  524.             private void addViewButton(TableCell colChannel, string mode, string text)
  525.             {
  526.                 LinkButton viewButton = new LinkButton();
  527.                 viewButton.Text = "<span>" + HttpContext.Current.Server.HtmlEncode(text) + "</span>";
  528.                 viewButton.CssClass = "btn-normal-tiny";
  529.                 viewButton.Style.Add("float", mode);
  530.                 viewButton.ID = "view" + mode + "button";
  531.                 colChannel.Controls.Add(viewButton);
  532.  
  533.                 colChannel.Controls.Add(new LiteralControl("<div style='padding-top: 4px'>" + colChannel.Text + "<div/>"));
  534.             }
  535.         }
  536.         
  537.         private void GetCookieValues()
  538.         {   
  539.             HttpCookie cookie = Request.Cookies["minuteSpan"];
  540.             minuteSpan = cookie != null ? Convert.ToInt32(cookie.Value) : 120;
  541.  
  542.             cookie = Request.Cookies["minuteInterval"];
  543.             minuteInterval = cookie != null ? Convert.ToInt32(cookie.Value) : 30;
  544.  
  545.             cookie = Request.Cookies["minuteSlice"];
  546.             minuteSlice = cookie != null ? Convert.ToInt32(cookie.Value) : 1;
  547.  
  548.             cookie = Request.Cookies["TimelineRepeat"];
  549.             TimelineRepeat = cookie != null ? Convert.ToInt32(cookie.Value) : 10;
  550.  
  551.             cookie = Request.Cookies["useChannelIcons"];
  552.             useChannelIcons = cookie != null ? cookie.Value : "chnlText";
  553.  
  554.             cookie = Request.Cookies["channelIconHeight"];
  555.             channelIconHeight = cookie != null ? cookie.Value : "";
  556.  
  557.             cookie = Request.Cookies["channelIconWidth"];
  558.             channelIconWidth = cookie != null ? cookie.Value : "";
  559.             
  560.          }
  561.  
  562.     }
  563. }
  564.